<?PHP
##################################
/* 溯源查询 */
##################################

class Admin_source_search_Controller extends controller_admin{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','溯源管理 - 溯源查询');
		$this->assign('menuid1','7');
		$this->assign('menuid2','4');
		//权限判断
		factory::module('admin')->authSys('source_search');
	}
	function index__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))			 $_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				 $_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = ''; 
			if(!isset($_POST['status']))    $_POST['status']  = ''; 
			if(!isset($_POST['b_date']))    $_POST['b_date']  = ''; 
			if(!isset($_POST['e_date']))    $_POST['e_date']  = ''; 
			
  	  #查询条件
			$sql_where = ' where b.deleted=0 '.func::speedsearch($_POST['keywords'],array( 'a.code' 	,'a.senddate' ));  
			if($_POST['state']!=''){
				$sql_where .= ' and b.state= "'.$_POST['state'].'"';	
			}
			if($_POST['status']!=''){
				$sql_where .= ' and a.status= "'.$_POST['status'].'"';	
			}
			if($_POST['b_date']!=''){
				$sql_where .= ' and a.senddate >= "'.$_POST['b_date'].'"';	
			}
			if($_POST['e_date']!=''){
				$sql_where .= ' and a.senddate <= "'.$_POST['e_date'].'"';	
			}
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(b.id) 
			from t_source b
			left join `t_sendpi` a on b.sendpi_id=a.id  
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select 
					b.*,
					a.code,
					a.senddate,
					a.sendfennum,
					a.status
				from t_source b
				left join `t_sendpi` a on b.sendpi_id=a.id  
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				foreach($rs as &$v){
					$v['vegenums'] = $db->loadresult('select count(*) from t_source_vege where source_id="'.$v['id'].'" ');
					$v['usernums'] = $db->loadresult('select count(*) from t_source_user where source_id="'.$v['id'].'" ');
				}
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();  
		}
	}
	/* 溯源查询 */
	public function view__(){
		$_GET=format::addslash_deep($_GET);
		$source_id = intval($_GET['id']);
		$db = factory::getDBO();
	  # 溯源信息
		$rs_source = $db->loadassoc('select 
					b.*,
					a.code,
					a.senddate,
					a.sendfennum,
					a.status
				from t_source b
				left join `t_sendpi` a on b.sendpi_id=a.id  
				where b.id="'.$source_id.'" ');
		if(!$rs_source){
			url::msgBox('没有查到任何记录！','-1');
		}
		#蔬菜溯源：供应阶段+生产阶段
    $rs_source_vege = factory::module('source')->getSourceVege($rs_source['id']);
		#会员配送溯源：配送阶段
    $rs_source_user = factory::module('source')->getSourceUser($rs_source['id']);
		
		
		if(count($rs_source_vege) <= 0){
			url::msgBox('当前批次暂未添加任何配送分支！','-1');
		}
		
		$pro_fields = factory::module('source')->getField('1');
		$make_fields = factory::module('source')->getField('2');
		$send_fields = factory::module('source')->getField('3');
	//debug::show($pro_fields,$make_fields,$send_fields,$rs_source_user,$rs_source_vege);
		  
	  
		$this->assign('rs_source',$rs_source);
		$this->assign('rs_source_user',$rs_source_user);
		$this->assign('rs_source_vege',$rs_source_vege); 
		$this->assign('pro_fields',$pro_fields); 
		$this->assign('make_fields',$make_fields); 
		$this->assign('send_fields',$send_fields);
		
	}
  
	/* 加载某一个会员的溯源信息 */
	public function loadUserSource__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			$user_id = intval($_POST['user_id']);
			$sendpi_id = intval($_POST['sendpi_id']);
			$rs_source = factory::module('source')->getSourceByUserSendpi($user_id,$sendpi_id);
		
			exit('{success:1,results:'.json_encode($rs_source).'}');
		}
		exit();
	}	
	  


 
}
?>